package com;

import java.util.HashMap;

import org.farng.mp3.MP3File;
import org.farng.mp3.TagConstant;
import org.farng.mp3.id3.ID3v1;

public class ID3TagsTranslit {
	private static HashMap<Integer, String> s_map;
	private static String s_title;
	private static String s_artist;
	static {
		s_map = new HashMap<Integer,String>();

		s_map.put(new Integer(224),"a");		//а
		s_map.put(new Integer(225),"b");		//б
		s_map.put(new Integer(226),"v");		//в
		s_map.put(new Integer(227),"g");		//г
		s_map.put(new Integer(228),"d");		//д
		s_map.put(new Integer(229),"e");		//е

		s_map.put(new Integer(184),"e");		//ё

		s_map.put(new Integer(230),"zh");		//ж
		s_map.put(new Integer(231),"z");		//з
		s_map.put(new Integer(232),"i");		//и
		s_map.put(new Integer(233),"j");		//й
		s_map.put(new Integer(234),"k");		//к
		s_map.put(new Integer(235),"l");		//л
		s_map.put(new Integer(236),"m");		//м
		s_map.put(new Integer(237),"n");		//н
		s_map.put(new Integer(238),"o");		//о
		s_map.put(new Integer(239),"p");		//п
		s_map.put(new Integer(240),"r");		//р
		s_map.put(new Integer(241),"s");		//с
		s_map.put(new Integer(242),"t");		//т
		s_map.put(new Integer(243),"u");		//у
		s_map.put(new Integer(244),"f");		//ф
		s_map.put(new Integer(245),"h");		//х
		s_map.put(new Integer(246),"c");		//ц
		s_map.put(new Integer(247),"ch");		//ч
		s_map.put(new Integer(248),"sh");		//ш
		s_map.put(new Integer(249),"sh");		//щ
		s_map.put(new Integer(250),"'");		//ъ

		s_map.put(new Integer(251),"y");		//ы
		s_map.put(new Integer(252),"'");		//ь
		s_map.put(new Integer(253),"e");		//э
		s_map.put(new Integer(254),"yu");		//ю
		s_map.put(new Integer(255),"ya");		//я

		//Upper
		s_map.put(new Integer(192),"A");		//А
		s_map.put(new Integer(193),"B");		//Б
		s_map.put(new Integer(194),"V");		//В
		s_map.put(new Integer(195),"G");		//Г
		s_map.put(new Integer(196),"D");		//Д
		s_map.put(new Integer(197),"E");		//Е

		s_map.put(new Integer(168),"E");		//Ё

		s_map.put(new Integer(198),"ZH");		//Ж
		s_map.put(new Integer(199),"Z");		//З
		s_map.put(new Integer(200),"I");		//И
		s_map.put(new Integer(201),"J");		//Й
		s_map.put(new Integer(202),"K");		//К
		s_map.put(new Integer(203),"L");		//Л
		s_map.put(new Integer(204),"M");		//М
		s_map.put(new Integer(205),"N");		//Н
		s_map.put(new Integer(206),"O");		//О
		s_map.put(new Integer(207),"P");		//П
		s_map.put(new Integer(208),"R");		//Р
		s_map.put(new Integer(209),"S");		//С
		s_map.put(new Integer(210),"T");		//Т
		s_map.put(new Integer(211),"U");		//У
		s_map.put(new Integer(212),"F");		//Ф
		s_map.put(new Integer(213),"H");		//Х
		s_map.put(new Integer(214),"C");		//Ц
		s_map.put(new Integer(215),"CH");		//Ч
		s_map.put(new Integer(216),"SH");		//Ш
		s_map.put(new Integer(217),"SH");		//Щ
		s_map.put(new Integer(218),"'");		//Ъ
		s_map.put(new Integer(219),"Y");		//Ы
		s_map.put(new Integer(220),"'");		//Ь
		s_map.put(new Integer(221),"E");		//Э
		s_map.put(new Integer(222),"YU");		//Ю
		s_map.put(new Integer(223),"YA");		//Я		
	}

	public static void setArtist(String art){
		s_artist = art;
	}
	
	public static void convert(String filename, String titlePrefix, String titleSuffix) {
		try{				
			//----init--------
			MP3File mp3 = new MP3File(filename);
			ID3v1 tag = mp3.getID3v1Tag();
			ID3v1 updTag = new ID3v1();
			//System.out.println("test : " + mp3.getFilenameTag().toString());
			System.out.println("-----------[" + filename + "] tags----------");			
			System.out.println();
			//----tags--------				
			//artist
			String currArtist = tag.getArtist();
			String newArtist;
			if(s_artist == null || s_artist.length() < 1){
				newArtist = translit(currArtist);
			}
			else{
				newArtist = s_artist;
			}
			tag.setArtist(newArtist);
			updTag.setArtist(newArtist);
			System.out.println("Artist     [" + 
					currArtist + "]->[" + newArtist + "]");
			
			//title
			String currTitle = tag.getTitle();
			String newTitle = translit(currTitle);
//			if(newTitle != null){
//				int ind = newTitle.indexOf(".");
//				String suf = newTitle.substring(ind + 2);
//				newTitle = suf.substring(0,1);
//				//String[] spl = newTitle.split(". ");
//				//System.out.println("debug --- [" + newTitle + "], [" + suf + "]");
//			}
			if(titlePrefix != null && titlePrefix.length() > 0){
				newTitle = titlePrefix + newTitle;
			}			
			if(titleSuffix != null && titleSuffix.length() > 0){
				newTitle = newTitle + titleSuffix;
			}			
			tag.setTitle(newTitle);
			System.out.println("Title      [" + 
					currTitle + "]-->[" + newTitle + "]");

			//identifier 
			String identifier = tag.getIdentifier();
			System.out.println();
			System.out.println("Identifier [" + identifier + "]");

			//----saving--------
			System.out.println("***** isCopyrightProtected : " + mp3.isCopyProtected());
			System.out.println("***** isPrivacy : " + mp3.isPrivacy());
			System.out.println("***** isProtection : " + mp3.isProtection());
//			mp3.setID3v1Tag(arg0)
			//mp3.delete(tag);
			mp3.setID3v1Tag(tag);
			mp3.save(TagConstant.MP3_FILE_SAVE_OVERWRITE);
//			mp3.save(TagConstant.MP3_FILE_SAVE_FIRST);
			//mp3.save(TagConstant.MP3_FILE_SAVE_WRITE);
			//mp3.save(TagConstant.MP3_FILE_SAVE_APPEND);
			System.out.println("-----------[" + filename + "] converted ok----------");
			System.out.println("----------------------------------------------------");
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	private static String translit(String tagval) {
		String eng = "";
		String s = null;
		Integer ind = null;
		for(int i = 0; i < tagval.length(); i++){
			char ch = tagval.charAt(i);
			ind = new Integer((int)ch);
			if(s_map.containsKey(ind)){
				s = s_map.get(ind);
				if(s.equals("'") & (i == tagval.length() - 1)){
					s = "";
				}
				if(s.equals("'") & (i + 1 < tagval.length()) 
						&& tagval.charAt(i + 1) == ' '){
					s = "";
				}
			}
			else{
				s = String.valueOf(ch);
			}
			//System.out.println("[" + ch + "," + ind + "] -> [" + s + "]");

			eng = eng.concat(s); 
		}
		return eng;
	}

}
